<?php

class Group {
    public static $group_names = array();
    public static $company=array();
    
    static public function group_names(){
        global $dbdefault;
        if (count(Group::$group_names)){
            return Group::$group_names;
        }else{
            $query="SELECT group_id, group_name FROM groups";
            $res = mysql_query($query,$dbdefault) or die(mysql_error());
            $group_names=array();
            while($r= mysql_fetch_assoc($res)){
                $group_names[$r['group_id']]=$r['group_name'];
            }
            Group::$group_names = $group_names;
            return Group::$group_names;
        }
    }

    static public function company(){
        global $dbdefault;
        if (count(Group::$company)){
            return Group::$company;
        }else{
            $query="SELECT idcustomers, namecustomer FROM customers";
            $res = mysql_query($query,$dbdefault) or die(mysql_error());
            $company=array();
            while($r= mysql_fetch_assoc($res)){
                $company[]=$r;
            }
            Group::$company = $company;
            return Group::$company;
        }
    }
    static public  function update($group_id,$data){       
        global $dbdefault;
        $query=sprintf("UPDATE groups SET group_name=%s, group_description=%s, idcustomer=%d WHERE group_id=%d",
            quote_smart($data['group_name']),quote_smart($data['group_description']),  qs($data['idcustomers']),$group_id);
        $result= @mysql_query($query,$dbdefault);
        if (!$result){
            return array('success'=>FALSE,'error'=> mysql_error(),'form_error'=>null);
        }else{
            return array('success'=>TRUE,'group_id'=>mysql_insert_id());
        }        
    }

    static public  function create($data){       
        $r = Group::validate($data);
        global $dbdefault;
        print_r($r);
        if (!$r['success']){
            return $r;
        }
        $query=sprintf(" INSERT INTO groups
            (group_name,group_description, idcustomer) VALUE(%s,%s,%d) ",
            quote_smart($data['group_name']),quote_smart($data['group_description']),  qs($data['idcustomers']));
        $result= @mysql_query($query,$dbdefault);
        if (!$result){
            return array('success'=>FALSE,'error'=> mysql_error(),'form_error'=>null);
        }else{
            return array('success'=>TRUE,'group_id'=>mysql_insert_id());
        }        
    }
    
    
    static public function delete($group_id){
        global $dbdefault;
        $query="DELETE from groups WHERE group_id=$group_id";
        $result=@mysql_query($query,$dbdefault);
        if (!$result){
            return array('success'=>FALSE,'error'=> mysql_error());
        }else{
            return array('success'=>TRUE);
        }
       
    }
    
    
    static public function validate($data)
    {
       global $dbdefault;
        $group_name=$data['group_name'];
       if (empty($group_name)){
           return array('success'=>FALSE,'error'=>'Group name cannot be empty!');
       }
       // Check there's no duplicate group_name
       $query=sprintf("SELECT count(*) as existed FROM groups WHERE group_name=%s",quote_smart($group_name));
       $result=mysql_query($query,$dbdefault) or die(__CLASS__.'->'.__METHOD__.':'.mysql_error());
       $existed=mysql_result($result,0,'existed');
       if ($existed){
          return array('success'=>FALSE,'error'=>'Group name existed!');
       }
       return array('success'=>TRUE);
    }
    
    
    static public function get_group_data($group_id){
        global $dbdefault;
        $query = "select a.group_id, a.group_name, a.group_description, b.idcustomers, b.namecustomer from groups a left join customers b on a.idcustomer=b.idcustomers WHERE group_id=$group_id";
        $result= @mysql_query($query,$dbdefault);
        if (!$result){
            return array('success'=>FALSE,'error'=> __METHOD__.':'.mysql_error());
        }else{
            $nr = mysql_num_rows($result);
            if ($nr){
                return array('success'=>TRUE,'data'=> mysql_fetch_assoc($result));
            }else{
                return array('success'=>FALSE,'error'=>"Group doesn't exist");
            }
        }
    }

    /*The belows are for group permission*/    
    static public function check_permission($group_id,$module,$action,$mode='exec'){
        $r = Group::get_permissions($group_id);
        $permissions = $r['permissions']; //Get permission anyway
        $per = $module."/".$mode."_".$action;
        //print_r($permissions);
        //echo '>>>'.$per.'<<<';
        if (key_exists($per,$permissions)){
            return $permissions[$per];
        }else{
            return 0;
        }
    }
    
   static public function check_permission_user($user_id,$module,$action,$mode='exec'){
       
       global $dbdefault;
       if($user_id==1){
           return 1;
       }else{
           $query=sprintf("SELECT group_id from users where user_id=%d",quote_smart($user_id));
            $result=mysql_query($query,$dbdefault);
            $group_id=mysql_result($result,0,'group_id');
            //return $group_id;
            if($group_id){
                $r= Group::check_permission($group_id,$module,$action,$mode='exec');
            }else{
                $r=-1;
            }
            return $r;
       }

    }
    
    static public function set_permissions($group_id,$permissions){
        global $dbdefault;
        $per = serialize($permissions);
        $query = "UPDATE groups SET group_permissions='$per' WHERE group_id=$group_id";
        $res = @mysql_query($query,$dbdefault);
        if ($res){
            return array('success'=>TRUE);
        }else{
            return array('success'=>FALSE,'error'=>  mysql_err());            
        }        
    }

    static public function get_permissions($group_id){
        global $dbdefault;
        $default_permissions = Modules::get_default_permissions();
        $query = "SELECT group_permissions FROM groups WHERE group_id=$group_id";
        $res = @mysql_query($query,$dbdefault);
        if ($res){
            if ($r = mysql_fetch_assoc($res)){
                $pers = unserialize($r['group_permissions']);
                //Need to merge with default_permission, incase modules change
                //print_r($pers);
                $permissions=array();
                foreach($default_permissions as $p => $v){
                    if (isset($pers[$p])){
                        $permissions[$p]=$pers[$p];
                    }else{
                        $permissions[$p]=$v;                        
                    }
                }
                return array('success'=>TRUE,'permissions'=>$permissions);                
            }else{
                return array('success'=>FALSE,'error'=>'Group does not exist!','permissions'=>$default_permissions);                                
            }
        }else{
            return array('success'=>FALSE,'error'=>  mysql_error(),'permissions'=>$default_permissions);            
        }                
    }
    
}